<template>
<div>
     <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <el-row>
            <el-col>
                <el-button type="primary">添加</el-button>
            </el-col>
        </el-row>
         <el-table :data="roleLists" border stripe class="role"> 
               
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-row v-for="item1 in scope.row.children" :key="item1.id" class="vcenter splice">
                            <el-col :span="5">
                                <el-tag type="primary"  closable @close="removeTag(scope.row ,item1.id)">{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <el-col :span="19">
                                <el-row v-for="item2 in item1.children" :key="item2.id" class="vcenter">
                                    <el-col :span="6">
                                        <el-tag type="success" closable @close="removeTag(scope.row ,item2.id)">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag v-for="item3 in item2.children" :key="item3.id" type="warning" closable @close="removeTag(scope.row ,item3.id)">{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop='roleDesc'></el-table-column>
                <el-table-column label="操作" width="180">
                 <template  slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                    <el-tooltip  effect="dark" content="操作" placement="top" :enterable="false">
                        <el-button type="warning" icon="el-icon-scissors" size="mini"  @click="showRightdialogVisible(scope.row)"></el-button>
                    </el-tooltip>
                  </template>
                </el-table-column>
            </el-table>
    </el-card>
                                                                <!-- 在每次关闭的时候先清空上次选中的复选框防止下次展开污染 -->
    <el-dialog title="提示" :visible.sync="RightdialogVisible" width="50%" @close="RefKeys=[]">
        <el-tree :data="rightList" :props="rightProps" show-checkbox node-key="id" default-expand-all
        :default-checked-keys="RefKeys" ref="treeRef"></el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="RightdialogVisible = false">取 消</el-button>
            <el-button type="primary"  @click="allowRights">确 定</el-button>
        </span>
    </el-dialog>
</div>
</template>

<script>
export default {
    data(){
        return {
            roleLists: [],
            RightdialogVisible: false,
            rightList: [],
            // 树形结构
            rightProps: {
            children: 'children',
            label: 'authName'
        },
        // 默认选中的选项
        RefKeys: [],
        // 提交角色的id
        roleId: ''
        }
    },
    created() {
        this.getRoleList()
    },
    methods: {
        // 为选中复选框提交
        async allowRights() {
            this.RightdialogVisible = true
            var keys = [
                ...this.$refs.treeRef.getCheckedKeys(),
                ...this.$refs.treeRef.getHalfCheckedKeys(),
            ]
            keys = keys.join(',')
            const {data: res} = await this.$http.post(`roles/${this.roleId}/rights`, {rids: keys})
            if (res.meta.status !== 200) return this.$message.error('修改失败')
            this.$message.success('提交成功')
            this.getRoleList()
            this.RightdialogVisible = false
        },
        // 通过递归把三级权限中所有的权限id保存到RefKey中
        getLeafKey(node, arr) {
            if (!node.children){
                return arr.push(node.id)
            }
            node.children.forEach(element => {
                this.getLeafKey(element, arr)
            });
        },
        // 对话框
        async showRightdialogVisible (right){
            this.roleId = right.id
            const {data: res} = await this.$http.get('rights/tree')
            if (res.meta.status !== 200) return this.$message.error('获取失败')
            this.rightList = res.data
            this.getLeafKey(right, this.RefKeys)
            this.RightdialogVisible = true

        },
        async getRoleList() {
            const {data: res} = await this.$http.get('roles')
            if (res.meta.status !== 200) this.$message.error('获取失败')
            this.roleLists = res.data
        },
        async removeTag(role ,id){
            const resu = await this.$confirm('是否删除', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).catch(err => err)
        if(resu !== 'confirm') return this.$message('撤销删除')
        const {data: res} = await this.$http.delete(`roles/${role.id}/rights/${id}`)
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        // 这样重新渲染全局会有不好的效果
        // this.getRoleList()
        // 因为在删除之后会返回一组全新的数据，所以只要把这一组全新的数据，渲染到当前分支，就不会全页面渲染了
        role.children = res.data
        }
    }
}
</script>
<style  scoped>
    .splice{
        margin-bottom: 25px;
        border-bottom: 1px dashed #000;
    }
    .vcenter{
        display: flex;
        align-items: center;
        
    }
</style>